<?php
namespace app\admin\controller;

use think\facade\View;

use app\admin\model\SysMenu as SysMenuModel;
use app\admin\model\SysRole as SysRoleModel;
use app\admin\model\SysUser as SysUserModel;

class SysRole extends Base
{
    public function index()
    {
        $data = SysRoleModel::field('id,name,permissions,create_time')->select();

        return View::fetch('index', [
            'data'=>$data
        ]);
    }

    public function info($id = 0) {
        if ($id != 0) {
            $data = SysRoleModel::find($id);
            // 已有权限数组
            $rolePowers = explode(',', $data->permissions);
        } else {
            $data = [
                'id' => 0,
                'name' => '',
                'permissions' => '',
                'note' =>''
            ];
            $rolePowers = [];
        }
        // var_dump($data);
        // exit;
        $sysMenuModel = new SysMenuModel();
        $dataPermission = obj_tree($sysMenuModel->select());
        return View::fetch('save', [
            'data' => $data,
            'rolePowers' => $rolePowers,
            'dataPermission' => $dataPermission
        ]);
    }

    public function save()
    {
        if ($this->app->request->isPost()) {
            $param = $this->app->request->param();
            if ($param['name'] == '') {
                $this->error("目录名不能为空");
            }
            $permissions = '';
            if (!empty($param['perm_check'])) {
                $perm_checks = $param['perm_check'];
                $permissions = implode(',', $perm_checks);
            }

            try {
                if ($param['id'] != 0) {
                    SysRoleModel::update([
                        'name' => $param['name'],
                        'permissions' => $permissions,
                        'note' => $param['note']
                    ], ['id' => $param['id']]);
                } else {
                    SysRoleModel::create([
                        'name' => $param['name'],
                        'permissions' => $permissions,
                        'note' => $param['note']
                    ]);
                }
            } catch (\Exception $e) {
                $msg = $e->getMessage();

                $this->error("错误提示:".$msg);
            }
            $this->success('操作成功', 'sys_role/index');
        }
    }

    public function delete($id = null)
    {
        if ($this->app->request->isAjax()) {
            $U_role_id = SysUserModel::where('id', $this->uid)->value('role_id');

            if (is_array($id)) {
                if (in_array('1', $id)) {
                    return ['status'=>0,'msg'=>'超级管理员无法删除'];
                }
                if (in_array($U_role_id, $id)) {
                    return ['status'=>0,'msg'=>'当前登录用户角色无法删除'];
                }
            } else {
                if ($id == 1) {
                    return ['status'=>0,'msg'=>'超级管理员无法删除'];
                }
                if ($id == $U_role_id) {
                    return ['status'=>0,'msg'=>'当前登录用户角色无法删除'];
                }
            }
            if (SysRoleModel::destroy($id)) {
                return ['code' => 1,'msg'=>'操作成功'];
            } else {
                return ['code' => 0,'msg'=>'操作失败'];
            }
        }
    }
}
